<template>
  <div class="mod-menu">
    <h1>三级分类页面</h1>
    <el-tree :default-expanded-keys="defaultExpandedKeys" node-key="catId" :expand-on-click-node="false" :data="data" :props="defaultProps">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            v-if="data.catLevel!=3"
            type="text"
            size="mini"
            @click="
              category = { sort: 0 };
              append(data);
            ">
            Append
          </el-button>
          <el-button type="text" size="mini" @click="update(data)">
            Update
          </el-button>
          <el-button 
          v-if="node.childNodes.length==0"
          type="text" size="mini" @click="remove(node, data)">
            Delete
          </el-button>
        </span>
      </span>
    </el-tree>

    <el-dialog title="分类编辑" :visible.sync="dialogFormVisible">
      <el-form :model="category">
        <el-form-item label="分类名称" label-width="120px">
          <el-input v-model="category.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      defaultExpandedKeys:[],
      category: {
        sort: 0,
      },
      dialogFormVisible: false,
      data: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
    };
  },
  components: {},
  activated() {
    this.getDataList();
  },
  methods: {
    //删除
    remove(node,data){
      console.log("参数为:",node,data);
      let ids = [data.catId];
      this.$http({
        url: this.$http.adornUrl(`/product/category/delete`),
        method: "post",
        data: this.$http.adornData(ids, false),
      }).then(({ data }) => {
        console.log(data);
        if (data.code == 0) {
          this.$message({
            showClose: true,
            message: "删除成功",
            type: "success",
          });
          this.getDataList();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    //修改弹框
    update(data) {
      //弹框开启
      this.dialogFormVisible = true;
      this.$http({
        url: this.$http.adornUrl(`/product/category/info/${data.catId}`),
        method: "get",
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        console.log(data);
        this.category = data.category;
      });
    },
    //保存
    save() {
      let methodUrl = "save";
      if (this.category.catId) {
        methodUrl = "update";
      }
      this.$http({
        url: this.$http.adornUrl(`/product/category/${methodUrl}`),
        method: "post",
        data: this.$http.adornData(this.category, false),
      }).then(({ data }) => {
        console.log(data);
        if (data.code == 0) {
          this.$message({
            showClose: true,
            message: "保存成功",
            type: "success",
          });
          this.dialogFormVisible = false;
          this.getDataList();
          this.defaultExpandedKeys = [this.category.parentCid];
        } else {
          this.$message.error("保存失败");
        }
      });
    },
    //编辑弹框
    append(data) {
      console.log("传递的参数为:", data);
      this.category.parentCid = data.catId;
      this.category.catLevel = data.catLevel + 1;
      this.dialogFormVisible = true;
      /**
       * 点击确认按钮时  总共要给5个字段赋值
       * id 自增
       * name 手动赋值
       * pid 被点击对象的id
       * catlevel 对象的层级加1
       *
       */
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      //renren吧axios封装成自己的工具类
      this.$http({
        url: this.$http.adornUrl("/product/category/tree/list"),
        method: "get",
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        console.log(data);
        this.data = data.data;
      });
    },
  },
};
</script>
